package com.bdjk.bdmw.InfoGath.business.LBS.service.impl;

import com.bdjk.bdmw.InfoGath.business.LBS.dao.BaseStationDAO;
import com.bdjk.bdmw.InfoGath.business.LBS.dao.impl.BaseStationDAOImpl;
import com.bdjk.bdmw.InfoGath.business.LBS.service.LBSLocationService;
import com.bdjk.bdmw.InfoGath.common.model.LBSModel;
import com.bdjk.bdmw.InfoGath.common.service.LBStestImpl;
import com.bdjk.bdmw.InfoGath.exception.BusinessException;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class LBSLocationServiceImpl  implements LBSLocationService{
  static final double PI = 3.14159265359D;
  private double longitude;
  private double latitude;
  private BaseStationDAO baseStationDAO = new BaseStationDAOImpl();
  
  public double[] getTermLocation(List<LBSModel> lbsModel)
  {
    double[] pos = new double[2];
    List<List<String>> rs = new ArrayList();
    for (int i = 0; i < lbsModel.size(); i++) {
      try
      {
        List<Object> temp = this.baseStationDAO.getBaseStationLocation(((LBSModel)lbsModel.get(i)).getMnc(), ((LBSModel)lbsModel.get(i)).getLacId(), ((LBSModel)lbsModel.get(i)).getCellId());
        System.out.print(((LBSModel)lbsModel.get(i)).getLacId() + " " + ((LBSModel)lbsModel.get(i)).getCellId());
        if (temp.size() > 0)
        {
          List<String> rstemp = new ArrayList();
          for (Object it : temp) {
            rstemp.add(it.toString());
          }
          rstemp.add(String.valueOf(((LBSModel)lbsModel.get(i)).getIntense()));
          rstemp.add(String.valueOf(((LBSModel)lbsModel.get(i)).getFrequency()));
          rs.add(rstemp);
          
          System.out.print((String)rstemp.get(0) + "  " + (String)rstemp.get(1));
          
          ((LBSModel)lbsModel.get(i)).setLongitude(Double.valueOf((String)rstemp.get(0)).doubleValue());
          ((LBSModel)lbsModel.get(i)).setLatitude(Double.valueOf((String)rstemp.get(1)).doubleValue());
          LBStestImpl test = new LBStestImpl((LBSModel)lbsModel.get(i));
          test.save();
        }
        else
        {
          LBStestImpl test = new LBStestImpl((LBSModel)lbsModel.get(i));
          test.save();
          lbsModel.remove(i);
          i--;
        }
        System.out.println();
      }
      catch (BusinessException e)
      {
        e.printStackTrace();
      }
    }
    if (lbsModel.size() <= 0) {
      return pos;
    }
    pos = getTermLocations(rs);
    System.out.println(pos[0] + ":  " + pos[1]);
    
    return pos;
  }
  
  public double[] getTermLocations(List<List<String>> basepos)
  {
    int max_location = basepos.size();
    int count = basepos.size();
    double[] distance_weight = new double[max_location];
    double distance_sum = 0.0D;
    double[] lat = new double[max_location];
    double[] lon = new double[max_location];
    for (int i = 0; i < count; i++)
    {
      lon[i] = (Double.valueOf((String)((List)basepos.get(i)).get(0)).doubleValue() / 180.0D * 3.14159265359D);
      lat[i] = (Double.valueOf((String)((List)basepos.get(i)).get(1)).doubleValue() / 180.0D * 3.14159265359D);
      
      double rxlevel = Double.valueOf((String)((List)basepos.get(i)).get(2)).doubleValue();
      double freq = Double.valueOf((String)((List)basepos.get(i)).get(3)).doubleValue();
      if (freq < 1.0D) {
        freq = 1000.0D;
      }
      distance_weight[i] = Math.pow(10.0D, (130.0D + rxlevel - 20.0D * Math.log10(freq)) / 20.0D);
      System.out.println(rxlevel + ": " + distance_weight[i]);
      distance_sum += distance_weight[i];
    }
    double x = 0.0D;
    double y = 0.0D;
    double z = 0.0D;
    for (int i = 0; i < count; i++)
    {
      x += Math.cos(lat[i]) * Math.cos(lon[i]) * distance_weight[i];
      y += Math.cos(lat[i]) * Math.sin(lon[i]) * distance_weight[i];
      z += Math.sin(lat[i]) * distance_weight[i];
    }
    double x_avg = x / distance_sum;
    double y_avg = y / distance_sum;
    double z_avg = z / distance_sum;
    

    double lat_avg = Math.atan(z_avg / Math.sqrt(x_avg * x_avg + y_avg * y_avg)) * 180.0D / 3.14159265359D;
    double lon_avg = Math.atan(y_avg / x_avg) * 180.0D / 3.14159265359D;
    if (lon_avg < 0.0D) {
      lon_avg += 180.0D;
    }
    double[] pos = new double[2];
    pos[0] = lon_avg;
    pos[1] = lat_avg;
    String longitude = String.valueOf(pos[0]);
    String latitude = String.valueOf(pos[1]);
    int startlon = longitude.indexOf(".");
    int startlat = latitude.indexOf(".");
    if (longitude.length() >= startlon + 7) {
      longitude = longitude.substring(0, startlon + 7);
    }
    if (latitude.length() >= startlat + 7) {
      latitude = latitude.substring(0, startlat + 7);
    }
    pos[0] = Double.valueOf(longitude).doubleValue();
    pos[1] = Double.valueOf(latitude).doubleValue();
    return pos;
  }
}
